﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCommerce.ManagementBase.Common.Base.Common;
using MyCommerce.Presentation.Stores.Presenters;
using MyCommerce.Presentation.Stores.IViews;
using Innova.Microfinanzas.BusinessEntities;
using Innova.FoundationFramework.DataTypes;
using Infragistics.Win.UltraWinGrid;

namespace MyCommerce.Windows.Stores.Views
{
    public partial class PrototypePointOfSalesView : BaseForm, IPointOfSales
    {
        private PointOfSalesPresenter pPresenter;

        #region CONSTRUCTOR

        public PrototypePointOfSalesView()
        {
            InitializeComponent();
            pPresenter = new PointOfSalesPresenter(this);
        }

        #endregion

        #region EVENTS

        private void StoreView_Load(object sender, EventArgs e)
        {
            if (Context.DealerStoreId == 0)
            {
                Context.ShowErrorMessageBox("El empleado no tiene una tienda asignada.", "IMPORTANTE");
                this.Enabled = false;
            }
            if (OnLoadView != null)
                OnLoadView(this, new GenericEventArgs<long>(Context.UserId));
        }

        private void btnAddProduct_Click(object sender, EventArgs e)
        {
            if (OnSearchProduct != null)
                OnSearchProduct(this, new GenericEventArgs<string>(txtSearchProduct.Text));
        }

        private void btnSearchProduct_Click(object sender, EventArgs e)
        {
            if (OnSearchProduct != null)
                OnSearchProduct(this, new GenericEventArgs<string>(txtSearchProduct.Text));
        }

        private void btnDeleteProduct_Click(object sender, EventArgs e)
        {
            if (grvSalesDetail.ActiveRow != null && grvSalesDetail.ActiveRow.Selected)
            {
                pColSalesOrderDetail.Remove((SalesOrderDetail)grvSalesDetail.ActiveRow.ListObject);
                grvSalesDetail.DataSource = pColSalesOrderDetail;
                grvSalesDetail.DataBind();
                TotalCalculate();
            }
        }

        private void txtSearchProduct_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                if (OnSearchProduct != null)
                    OnSearchProduct(this, new GenericEventArgs<string>(txtSearchProduct.Text));
            }
        }

        private void btnCobrar_Click(object sender, EventArgs e)
        {
            if (ColProductToSale == null)
            {
                Context.ShowInformationMessageBox("No tiene productos en su venta para cobrar", "AVISO");
                return;
            }
            if (ColProductToSale.Count == 0)
            {
                Context.ShowInformationMessageBox("No tiene productos en su venta para cobrar", "AVISO");
                return;
            }

            if (OnSale != null)
                OnSale(this, e);
        }

        private void grvSalesDetail_AfterCellUpdate(object sender, Infragistics.Win.UltraWinGrid.CellEventArgs e)
        {
            if (e.Cell.Column.Key == "OrderQuantity"
                || e.Cell.Column.Key == "UnitPriceReal")
            {
                e.Cell.Row.Cells["LineTotal"].Value = Convert.ToDecimal(e.Cell.Row.Cells["OrderQuantity"].Value) * Convert.ToDecimal(e.Cell.Row.Cells["UnitPriceReal"].Value);
                TotalCalculate();
            }
        }

        #endregion

        #region METHODS

        public void ClearWindow()
        {
            txtSearchProduct.Text = "";
            pColSalesOrderDetail = new List<SalesOrderDetail>();
            grvSalesDetail.DataSource = new List<SalesOrderDetail>();
            grvSalesDetail.DataBind();
            customerSearchControl1.ClearControl();
            TotalCalculate();
        }

        private void TotalCalculate()
        {
            decimal totalSale = 0;
            foreach (UltraGridRow objRow in grvSalesDetail.Rows)
            {
                totalSale += Convert.ToDecimal(objRow.Cells["LineTotal"].Value);
            }
            txtTotal.Text = totalSale.ToString("##,###0.00");
        }

        #endregion

        #region IPointOfSales Members

        private int countProductToSale = 0;
        List<SalesOrderDetail> pColSalesOrderDetail = new List<SalesOrderDetail>();
        public SalesOrderDetail AddProductToSale
        {
            set
            {
                foreach (SalesOrderDetail item in pColSalesOrderDetail)
                {
                    if (item.IdProduct == value.IdProduct)
                    {
                        Context.ShowInformationMessageBox("Ya esta este producto en la lista de venta.", "AVISO");
                        return;
                    }
                }

                countProductToSale++;
                value.NumberDetail = countProductToSale;
                pColSalesOrderDetail.Add(value);
                grvSalesDetail.DataSource = pColSalesOrderDetail;
                grvSalesDetail.DataBind();
            }
        }

        public List<SalesOrderDetail> ColProductToSale
        {
            get
            {
                List<SalesOrderDetail> pColProductToSale = new List<SalesOrderDetail>();
                foreach (UltraGridRow item in grvSalesDetail.Rows)
                {
                    SalesOrderDetail objSalesOrderDetail = new SalesOrderDetail();
                    objSalesOrderDetail = (SalesOrderDetail)item.ListObject;
                    pColProductToSale.Add(objSalesOrderDetail);
                }
                return pColProductToSale;
            }
        }

        public Product InvokeEditionView(string productToSearch)
        {
            Product result = null;
            Command commandForm = Context.GetCommand("SearchProduct");
            object formResult = Context.InvoqueCommandWithParams(commandForm, txtSearchProduct.Text, true);
            if (formResult != null)
                result = (Product)formResult;
            return result;
        }
        
        public bool InvokeSaleView(List<SalesOrderDetail> objSalesOrderDetail)
        {            
            bool result = false;
            Command commandForm = Context.GetCommand("SalesCharge");

            Dictionary<long, List<SalesOrderDetail>> paramDetail = new Dictionary<long, List<SalesOrderDetail>>();
            long idCustomer = -1;
            if (customerSearchControl1.ObjCustomerLoad != null)
                idCustomer = customerSearchControl1.ObjCustomerLoad.IdCustomer;
            paramDetail.Add(idCustomer, objSalesOrderDetail);

            object formResult = Context.InvoqueCommandWithParams(commandForm, paramDetail, true);
            if (formResult != null)
                result = Convert.ToBoolean(formResult);
            return result;
        }

        public event EventHandler OnSale;

        public event GenericEventHandler<long> OnLoadView;

        public event GenericEventHandler<string> OnSearchProduct;

        #endregion

        private void ultraButton8_Click(object sender, EventArgs e)
        {
            ClearWindow();
        }

        private void ultraButton1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnSalesAndDevoluciones_Click(object sender, EventArgs e)
        {
            Command objCommand = Context.GetCommand("SalesChange");
            Context.InvoqueCommand(objCommand, true);
        }
                
    }
}
